package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.MaintenanceRecordDao;
import cn.topevery.garden.entity.po.MaintenanceRecord;
import cn.topevery.garden.entity.query.MaintenanceRecordQuery;
import cn.topevery.garden.entity.vo.MaintenanceRecordVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 保养记录
 *
 * @author code-generate
 * @date 2022-02-17
 */
@Repository
public class MaintenanceRecordDaoImpl extends BaseDaoImpl<MaintenanceRecord> implements MaintenanceRecordDao {
    @Override
    public <P extends DbCsp> List<MaintenanceRecord> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<MaintenanceRecordVO> listPage(MaintenanceRecordQuery query) {
        String table = "t_maintenance_record tmr left join t_gre_tree_famous tgt on tgt.c_id=tmr.c_tree_id " +
                "left join t_sys_user tsu on tmr.c_db_create_id=tsu.c_id";

        String columns = "tmr.*, tgt.c_code,tgt.c_name,tgt.c_lng,tgt.c_lat,tsu.c_user_name";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgt.c_code,tmr.c_date desc");
        sqlQueryMate
                .where(true, "tmr.c_db_status", ScriptConditionEnum.Eq, 0)
        ;
        if (ObjectUtils.isNotEmpty(query.getDate())){
            sqlQueryMate
                    .where(true, "tmr.c_date", ScriptConditionEnum.GreaterThanEq, query.getDate())
                    .where(true,"tmr.c_date", ScriptConditionEnum.LessThan, query.getDate().plusDays(1))
                    ;
        }

        return getDataPager(sqlQueryMate, pageSize, pageIndex, MaintenanceRecordVO.class);
    }

    @Override
    public MaintenanceRecord getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public Integer getId(String recordCode) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_maintenance_record tmr").setColumns("tmr.c_id id");
        sqlQueryMate.where(true, "tmr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tmr.c_record_code", ScriptConditionEnum.Eq, recordCode);
        Map<String, Object> map = getOneMap(sqlQueryMate);
        if(ObjectUtils.isEmpty(map)){
            return 0;
        }
        Object value = map.get("id");
        if(value instanceof Integer){
            return (Integer) value;
        }
        return 0;
    }
}
